const {sequelize} = require('../../core/db')
const {Sequelize, Model, Op} = require('sequelize');
const {
    Favor
} = require('./favor')

class HotBook extends Model {
   static async getAll() {
       const books = await HotBook.findAll({
            order: [
                'index'
            ]
       })
       const ids = [];
       books.forEach((book) => {
        ids.push(book.id);
       });
       
       const favors = await Favor.findAll({
            where:{
                art_id:{
                    [Op.in]:ids,
                },
                type:400
            },
            group:['art_id'],
            attributes:['art_id', [Sequelize.fn('COUNT','*'),'count']]
      })
      
      books.forEach((book) => {
        const bookValue = HotBook._getEachBookStatus(book, favors);
        return bookValue;
      })
      return books;
   }

   static _getEachBookStatus(book, favors) {
       let count = 0;
       favors.forEach((favors) => {
         if(book.id === favors.art_id) {
            count = favors.get('count');
         }
       });
       book.setDataValue('count', count);
       return book
   }


}

HotBook.init({
   index: Sequelize.INTEGER,
   image: Sequelize.STRING,
   author: Sequelize.STRING,
   title: Sequelize.STRING
}, {
    sequelize,
    tableName: 'hot_book'
})

module.exports = {
    HotBook 
}